package com.olap.starter.core.base;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.olap.starter.core.exception.OnlineException;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author WBG
 * @date 2022/8/30 22:03
 * @describe
 */
public class BaseService<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {
    public BaseService() {

    }

    public boolean removeById(Serializable id) {
        return SqlHelper.retBool(this.logicDeleteById(id));
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    public boolean removeByIds(Collection idList) {
        Iterator var2 = idList.iterator();

        boolean flg;
        do {
            if (!var2.hasNext()) {
                return true;
            }

            Object id = var2.next();
            flg = SqlHelper.retBool(this.logicDeleteById((Serializable)id));
        } while(flg);

        throw new OnlineException("删除失败，存在已删除的数据,请刷新后重试！");
    }

    protected int logicDeleteById(Serializable id) {
        return this.baseMapper.deleteById(id);
    }

    public boolean removeByWrapper(Wrapper<T> wrapper) {
        return SqlHelper.retBool(this.baseMapper.delete(wrapper));
    }


}
